﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ksp.Controller
{
    internal static class SubSectionParser
    {
        private const String BracketOpen = "{";
        private const String BracketClose = "}";

        internal static List<String> ParseSubSection(List<String> lines, ref int lineNo)
        {
            List<String> subSectionLines = new List<string>();
            int bracketsOpen = 0;
            bool iterate = true;
            try
            {
                while (iterate && lineNo < lines.Count-1)
                {
                    lineNo++; // skip keyword line
                    switch (lines[lineNo])
                    {
                        case BracketOpen:
                            bracketsOpen++;
                            subSectionLines.Add(lines[lineNo].Replace("\t", null));
                            break;
                        case BracketClose:
                            bracketsOpen--;
                            subSectionLines.Add(lines[lineNo].Replace("\t", null));
                            break;
                        default:
                            if (bracketsOpen > 0)
                                subSectionLines.Add(lines[lineNo].Replace("\t", null));
                            else
                            {
                                iterate = false;
                                lineNo--;
                                //lineNo = lines.Count - 2; // escape
                            }
                            break;
                    }
                    
                }
            }
            catch(Exception ex)
            {}
            

            return subSectionLines;
        }
    }
}
